//
//  YSChannelItem.m
//  YSKitDemo
//
//  Created by MOLBASE on 2018/8/14.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSChannelItem.h"

@implementation YSChannelItem {
    UILabel *_textLabel;
    CAShapeLayer *_borderLayer;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self buildUI];
    }
    return self;
}

- (void)buildUI {
    self.userInteractionEnabled = YES;
    self.layer.cornerRadius     = 5.0;
    self.backgroundColor        = [self backgroundColor];

    _textLabel                           = [[UILabel alloc] initWithFrame:self.bounds];
    _textLabel.textAlignment             = NSTextAlignmentCenter;
    _textLabel.textColor                 = [UIColor colorWithRed:40/255.0f green:40/255.0f blue:40/255.0f alpha:1];
    _textLabel.adjustsFontSizeToFitWidth = YES;
    _textLabel.userInteractionEnabled    = YES;
    [self addSubview:_textLabel];

    [self addBorderLayer];
}

- (void)addBorderLayer {
    _borderLayer                 = [CAShapeLayer layer];
    _borderLayer.bounds          = self.bounds;
    _borderLayer.position        = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path            = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth       = 1.f;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor       = [UIColor clearColor].CGColor;
    _borderLayer.strokeColor     = self.backgroundColor.CGColor;
    [self.layer addSublayer:_borderLayer];
    _borderLayer.hidden = YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _textLabel.frame = self.bounds;
}


#pragma mark - setter
- (void)setTitle:(NSString *)title {
    _title          = title;
    _textLabel.text = title;
}

- (void)setIsMoving:(BOOL)isMoving {
    _isMoving = isMoving;
    if (_isMoving) {
        self.backgroundColor = [UIColor clearColor];
        _borderLayer.hidden  = NO;
    } else {
        self.backgroundColor = [self backgroundColor];
        _borderLayer.hidden  = YES;
    }
}

- (void)setIsFixed:(BOOL)isFixed {
    _isFixed = isFixed;
    if (isFixed) {
        _textLabel.textColor = [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];
    } else {
        _textLabel.textColor = [UIColor colorWithRed:40/255.0f green:40/255.0f blue:40/255.0f alpha:1];
    }
}

- (UIColor *)backgroundColor {
    return [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1];
}

@end
